package com.alogic.cron.matcher;

import com.alogic.cron.CronMatcher;
import com.anysoft.util.DefaultProperties;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;

/**
 * 调度时间匹配器(按次数匹配)
 *
 * @since 1.6.13.7 [20200720 duanyy] <br>
 */
public class Counter extends CronMatcher.Abstract {
	
	/**
	 * 总的调度次数，可通过参数count配置
	 */
	protected long count = 1;
	
	/**
	 * 调度间隔，可通过参数interval配置
	 */
	protected long interval = 1000;
	
	/**
	 * 已调度次数
	 */
	protected long scheduled_count = 0;

	public Counter(){
	}

	public Counter(String expr){
		DefaultProperties p = new DefaultProperties();
		p.loadFromString(expr,";","=");
		configure(p);
	}

	public final void configure(Properties p) {
		count = PropertiesConstants.getLong(p,"count",count,true);
		interval = PropertiesConstants.getLong(p,"interval",interval,true);
	}

	@Override
	public boolean match(long lastDate, long now) {
		if (count <= 0 || count > scheduled_count){
			//还可以继续
			if (lastDate <= 0 ? true :(now - lastDate >= interval)){
				System.out.println(scheduled_count);
				scheduled_count ++;
				return true;
			}
		}
		return false;
	}
}
